package com.int_tech.utils.poi;

import com.int_tech.facility.entity.Facility;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/23.
 */
public class ReadExcelFacilityCeiling {
    public static List<Facility> readExcel(MultipartFile file) {
        DecimalFormat decimalFormat = new DecimalFormat("0");
        List<Facility> list = new ArrayList<>();
        XSSFWorkbook workbook = null;

        try {
            // 读取Excel文件
            InputStream inputStream = file.getInputStream();
            workbook = new XSSFWorkbook(inputStream);
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 循环工作表
        for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
            XSSFSheet hssfSheet = workbook.getSheetAt(numSheet);
            if (hssfSheet == null) {
                continue;
            }
            // 循环行
            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                XSSFRow row = hssfSheet.getRow(rowNum);
                if (row == null) {
                    continue;
                }

                // 将单元格中的内容存入集合
                Facility facility = new Facility();

                XSSFCell cell = row.getCell(0);
                if (cell == null) {
                    continue;
                }
                facility.setFacilityNumber(cell.getStringCellValue());

                cell = row.getCell(1);
                if (cell == null) {
                    continue;
                }
                facility.setHedition(decimalFormat.format(cell.getNumericCellValue()));

                cell = row.getCell(2);
                if (cell == null) {
                    continue;
                }
                facility.setSedition(decimalFormat.format(cell.getNumericCellValue()));

                cell = row.getCell(3);
                if (cell == null) {
                    continue;
                }
                facility.setSimBrand(cell.getStringCellValue());

                cell = row.getCell(4);
                if (cell == null) {
                    continue;
                }
                facility.setSimNumber(decimalFormat.format(cell.getNumericCellValue()));

                cell = row.getCell(5);
                if (cell == null) {
                    continue;
                }
                facility.setCarNumber(cell.getStringCellValue());

                cell = row.getCell(6);
                if (cell == null) {
                    continue;
                }
                facility.setRfidNumber(decimalFormat.format(cell.getNumericCellValue()));

                cell = row.getCell(7);
                if (cell == null) {
                    continue;
                }
                facility.setRemake(cell.getStringCellValue());

                list.add(facility);
            }
        }
        return list;
    }
}
